package com.health.mapp.impl;

import com.health.db.model.*;
import com.health.db.service.order.IRegisterOrderService;
import com.health.db.service.order.ISubRegisterOrderService;
import com.health.db.service.sm.IAlipayService;
import com.health.db.service.sm.IGoodsOrderService;
import com.health.db.service.user.IGoodsPriceListService;
import com.health.db.service.user.IUserCardListService;
import com.health.mapp.bean.*;
import com.health.mapp.interImpl.PayActionServerImpl;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;
import org.apache.http.util.TextUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by YYY-Work on 2017/6/15.
 */
public class PayControllerAliWapImpl implements ActionInterface {


    //service import
    private IRegisterOrderService registerOrderService;
    private IGoodsPriceListService goodsPriceListService;
    private IAlipayService alipayService;
    private ISubRegisterOrderService subRegisterOrderService;
    private IGoodsOrderService goodsOrderService;

    private IUserCardListService userCardListService;

    @Override
    public Response execute(Header header, Object reqBean) throws Exception {

        PayControllerAliWapReq req = (PayControllerAliWapReq) reqBean;
        PayControllerAliWapResp _resp = new PayControllerAliWapResp();
        Long orderId = req.getOrderId();
        String goodsType = req.getGoodsType();

        if (orderId == 0) {
            _resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
            _resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
            return _resp;
        } else {

            String goodsName = "";
            BigDecimal orderFee = new BigDecimal(0);
            String temOrderId = String.valueOf(orderId); // 订单号

            //子订单
            if (goodsType.equals(Constant.CONSTANTS_PAY_ACTION_ID_43.toString())) {

                temOrderId = temOrderId + "-" + String.valueOf(Constant.CONSTANTS_PAY_ACTION_ID_43);

                SubRegisterOrder currGoodsOrder = new SubRegisterOrder();
                currGoodsOrder = subRegisterOrderService.selectByPrimaryKey(orderId);


                if (null != currGoodsOrder
                        && currGoodsOrder.getRunstatusid().intValue() == Constant.CONSTANTS_ORDER_RUNSTATUS_ID_1.intValue()) {

                    //查询到用户可用的卡列表，并返回离失效时间最近的一张卡
                    UserCardListExt queryUserCardListExt = new UserCardListExt();

                    queryUserCardListExt.setOwnerid(currGoodsOrder.getUserId());
                    //增加剩余时间（次数，余额）校验，如果余额已为0 ，或是余额不够此次消费，则不返回
                    if (currGoodsOrder.getSubOrderType().intValue() == 4) {
                        queryUserCardListExt.setLefttime04(new BigDecimal(1));
                        goodsName = "送药服务";
                        List<UserCardList> list = userCardListService.selectUserCardLists(queryUserCardListExt);
                        if (list.size() > 0) {
                            SubRegisterOrder temp = new SubRegisterOrder();
                            orderFee = currGoodsOrder.getMediCharge();
                            temp.setSubOrderId(orderId);
                            temp.setExpendCard((short) 1);
                            subRegisterOrderService.updateOrderSelective(temp);
                        } else {

                            orderFee = currGoodsOrder.getMediCharge().add(currGoodsOrder.getServerCharge());
                        }

                    } else if (currGoodsOrder.getSubOrderType().intValue() == 5) {
                        goodsName = "挂号服务";
                        orderFee = currGoodsOrder.getServerCharge();
                    } else {
                        _resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
                        _resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
                        return _resp;
                    }
                } else {
                    _resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
                    _resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
                    return _resp;
                }


            } else
                //商品订单
                if (goodsType.equals(Constant.CONSTANTS_PAY_ACTION_ID_41.toString())) {


                    temOrderId = temOrderId + "-" + String.valueOf(Constant.CONSTANTS_PAY_ACTION_ID_41);

                    GoodsOrder currGoodsOrder = new GoodsOrder();
                    currGoodsOrder = goodsOrderService.selectByPrimaryKey(orderId);

                    if (null != currGoodsOrder
                            && currGoodsOrder.getRunstatusid().intValue() == Constant.CONSTANTS_ORDER_RUNSTATUS_ID_1.intValue()) {
                        goodsName = currGoodsOrder.getGoodsName();
                        orderFee = currGoodsOrder.getTotal();

                    } else {
                        _resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
                        _resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
                        return _resp;
                    }


                } else {

                    RegisterOrder orderInfo = new RegisterOrder();

                    orderInfo.setRegisterid(orderId);
                    RegisterOrder currRegisterOrder = registerOrderService.selectSimpleSingleByOrderId(orderId);

                    // 判断订单状态 订单不存在或者是免费订单
                    if (currRegisterOrder == null
                            || currRegisterOrder.getRegistertypeid().intValue() == Constant.CONSTANTS_ORDER_FREE_ID_0.intValue()) {
                        _resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
                        _resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
                        return _resp;
                    } else {

                        /******************* 订单数据 **********************************/
                        orderFee = currRegisterOrder.getRegisterTotal(); // 价格
                        /******************* 订单数据 **********************************/

                        GoodsPriceList example = new GoodsPriceList();

                        Long docId = currRegisterOrder.getAccepterid();
                        if (null != docId) {
                            example.setSerialIdno(currRegisterOrder.getAccepterid().toString());
                        } else {
                            example.setSerialIdno(null);
                        }
                        example.setGoodsid(currRegisterOrder.getGoodsid().intValue());
                        example.setColumnid((short) 1);
                        GoodsPriceListTemp goodsPriceList = goodsPriceListService.selectByGoodsIdDocIdSingle(example);

                        if (null != goodsPriceList) {
                            // 商品名称
                            goodsName = goodsPriceList.getGoodsName(); // 商品名称;

                        } else {
                            _resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_4001);
                            _resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_4001_NOTE);
                            return _resp;
                        }
                    }
                }

            PayActionServerImpl impl = new PayActionServerImpl();

            Long payCashId = impl.cleanExpiredPayment(temOrderId, Constant.CONSTANTS_PAYMENT_ALIPAY);

            String payHtml=alipayService.applyWapPay(goodsName,
                    String.valueOf(payCashId) + "-" + String.valueOf(temOrderId),
                    String.valueOf(orderFee.setScale(2, BigDecimal.ROUND_HALF_UP)),"1");

            if (TextUtils.isEmpty(payHtml))
            {
                _resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
                _resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
            }else
            {
                _resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
                _resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
                _resp.setPayHtml(payHtml);
            }

        }
        _resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
        _resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);

        return _resp;
    }

    public IRegisterOrderService getRegisterOrderService() {
        return registerOrderService;
    }

    public void setRegisterOrderService(IRegisterOrderService registerOrderService) {
        this.registerOrderService = registerOrderService;
    }

    public IGoodsPriceListService getGoodsPriceListService() {
        return goodsPriceListService;
    }

    public void setGoodsPriceListService(IGoodsPriceListService goodsPriceListService) {
        this.goodsPriceListService = goodsPriceListService;
    }

    public IAlipayService getAlipayService() {
        return alipayService;
    }

    public void setAlipayService(IAlipayService alipayService) {
        this.alipayService = alipayService;
    }

    public ISubRegisterOrderService getSubRegisterOrderService() {
        return subRegisterOrderService;
    }

    public void setSubRegisterOrderService(ISubRegisterOrderService subRegisterOrderService) {
        this.subRegisterOrderService = subRegisterOrderService;
    }

    public IGoodsOrderService getGoodsOrderService() {
        return goodsOrderService;
    }

    public void setGoodsOrderService(IGoodsOrderService goodsOrderService) {
        this.goodsOrderService = goodsOrderService;
    }

    public IUserCardListService getUserCardListService() {
        return userCardListService;
    }

    public void setUserCardListService(IUserCardListService userCardListService) {
        this.userCardListService = userCardListService;
    }
}
